package com.example.accounting.provider;

import com.example.accounting.ResourceTable;
import com.example.accounting.model.AccountInfo;

import com.example.accounting.model.AccountOperate;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.HashMap;
import java.util.List;


public class AccountItemProvider extends BaseItemProvider {
    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 900;

    private static final int DIALOG_CORNER_RADIUS = 30;

    private CommonDialog commonDialog;

    private List<AccountInfo> accountInfoShow;

    private final Context context;

    private static AccountOperate accountOperate;


    public AccountItemProvider(Context context, List<AccountInfo> contactData) {
        this.accountInfoShow = contactData;
        this.context = context;
        accountOperate = new AccountOperate(context);


    }

    @Override
    public int getCount() {
        return accountInfoShow.size();
    }

    @Override
    public Object getItem(int position) {
        return accountInfoShow.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return getItemComponent(position);
    }

    private Component getItemComponent(int position) {
        return getComponent(position);
    }

    private Component getComponent(int position) {
        AccountInfo contact = accountInfoShow.get(position);
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_card_day, null, false);
        Text date = (Text) container.findComponentById(ResourceTable.Id_date);
        Text type = (Text) container.findComponentById(ResourceTable.Id_type);
        Text info = (Text) container.findComponentById(ResourceTable.Id_info);
        Text money = (Text) container.findComponentById(ResourceTable.Id_money);

        date.setText(contact.getDate());
        type.setText(contact.getType());
        info.setText(contact.getInfo());
        money.setText(String.format("%.2f", contact.getMoney()));

        container.setClickedListener(component -> showContactDetailDialog(contact, position));
        return container;
    }

    private void showContactDetailDialog(AccountInfo contact, int position) {
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_layout, null, false);
        TextField date = (TextField) container.findComponentById(ResourceTable.Id_dialog_info_date);
        date.setText(contact.getDate());
        TextField type = (TextField) container.findComponentById(ResourceTable.Id_dialog_info_type);
        type.setText(contact.getType());
        TextField info = (TextField) container.findComponentById(ResourceTable.Id_dialog_info_info);
        info.setText(contact.getInfo());
        TextField money = (TextField) container.findComponentById(ResourceTable.Id_dialog_info_money);
        money.setText(String.valueOf(contact.getMoney()));
        Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);
        Button btnDelete = (Button) container.findComponentById(ResourceTable.Id_btn_delete);

        btnOk.setClickedListener(component -> saveEdit(contact.getAccountId(), date.getText(), type.getText(), info.getText(), money.getText(), position));
        btnDelete.setClickedListener(component -> {
            accountOperate.delete(contact.getAccountId());
            accountInfoShow.remove(contact);
            commonDialog.destroy();
            notifyDataChanged();
        });

        commonDialog = new CommonDialog(context);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();
    }

    private void saveEdit(int accountId, String date, String type, String info, String money, int position) {
        accountOperate.update(accountId, date, type, info, money);
        accountInfoShow.get(position).setDate(date);
        accountInfoShow.get(position).setType(type);
        accountInfoShow.get(position).setInfo(info);
        accountInfoShow.get(position).setMoney(Double.parseDouble(money));
        notifyDataChanged();
        commonDialog.destroy();
    }

    public void update() {
        accountInfoShow = accountOperate.selectAll();
        notifyDataChanged();
    }

    public HashMap<String, Double> getInOutCome() {
        double income = 0.0;
        double outcome = 0.0;
        for (AccountInfo accountInfo : accountInfoShow) {
            if (accountInfo.getMoney() >= 0) {
                income += accountInfo.getMoney();
            } else {
                outcome -= accountInfo.getMoney();
            }
        }

        HashMap<String, Double> hashMap = new HashMap<>();
        hashMap.put("income", income);
        hashMap.put("outcome", outcome);
        return hashMap;
    }

    public boolean insert(String date, double money, String type, String info, String account) {
        boolean a = accountOperate.insert(date, money, type, info, account);
        System.out.println(a);
        update();
        return a;

    }

    public void update(int year, int month) {
        accountInfoShow = accountOperate.select(year, month);
        notifyDataChanged();
    }

    public void update(String year, String month) {
        int year_val = Integer.parseInt(year.replaceFirst(".$", ""));
        int month_val = Integer.parseInt(month.replaceFirst(".$", ""));
        update(year_val, month_val);
    }
}
